<template>
  <div>
    <!-- <div>
    添加子部门, {{ showDialog }}
  </div>
  </div> -->

    <!-- 点击x，会触发close事件 -->
    <el-dialog
      :title="(isEdit ? '编辑':'添加') + '子部门'"
      :visible="showDialog"
      @close="hClose"
    >
      <el-form ref="form" label-width="120px" :rules="rules" :model="form">
        <el-form-item prop="name" label="部门名称">
          <el-input
            v-model="form.name"
            placeholder="2-10个字符"
            style="width: 80%"
            size="mini"
          />
        </el-form-item>

        <el-form-item prop="code" label="部门编码">
          <el-input
            v-model="form.code"
            placeholder="2-10个字符"
            style="width: 80%"
            size="mini"
          />
        </el-form-item>

        <el-form-item prop="managerId" label="部门负责人">
          <el-select
            v-model="form.managerId"
            placeholder="请选择负责人"
            style="width: 80%"
            size="mini"
          >
            <!-- 下拉框 -->
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="introduce" label="部门介绍">
          <el-input
            v-model="form.introduce"
            placeholder="1-100个字符"
            type="textarea"
            size="mini"
            :rows="4"
            style="width: 80%"
          />
        </el-form-item>

        <el-form-item>
          <!-- 按钮 -->
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button size="mini" type="primary" @click="addButton">确定</el-button>
              <el-button size="mini" @click="hClose">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addPost, getDepartment, getDepartmentList, updateDept, updatePut } from '@/api/department'
export default {
  props: {
    // 由父组件来控制显示和隐藏
    showDialog: {
      type: Boolean
    },
    id: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      list: [],
      form: {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10的字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              //  value 输入的内容
              console.log(value)
              //  api 部门列表
              const res = await getDepartment()
              // console.log(res.data)
              let data = res.data
              // 如果当前是编辑 先从data中排出自己
              if (this.isEdit) {
                data = res.data.filter(item => item.id !== this.id)
              }
              if (data.find((item) => item.name === value)) {
                callback(new Error('名字已占用 请重新输入'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10的字符', treigger: 'blur' },
          // 自定义校验
          {
            validator: async(rule, value, callback) => {
              //  value 输入的内容
              // console.log(value)
              //  api 部门列表
              const res = await getDepartment()
              // console.log(res.data)
              let data = res.data
              // 如果当前是编辑 先从data中排出自己
              if (this.isEdit) {
                data = res.data.filter(item => item.id !== this.id)
              }
              if (data.find((item) => item.name === value)) {
                callback(new Error('编码已占用已占用 请重新输入编码'))
              } else {
                callback()
              }
            }
          }
        ],
        managerId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDepartmenChange()
  },
  methods: {
    async getDetail() {
      //  发送修改部门数据请求
      const res = await updateDept(this.id)
      // console.log(res)
      // 数据回显
      this.form = res.data
    },
    async doAdd() {
      // 1. 调用api
      const res = await addPost({ ...this.form, pid: this.id })
      console.log(res)
      // 2. 提醒OK
      this.$message.success('添加成功')
      // 3. 关闭弹框  清空表单数据
      this.hClose()
      // 4. 通知父组件更新数据
      this.$emit('updateManager')
    },
    async doEdit() {
      // 1. 调用api
      await updatePut(this.form)
      // 2. 提醒OK
      this.$message.success('修改成功')
      // 3. 关闭弹框  清空表单数据
      this.hClose()
      // 4. 通知父组件更新数据
      this.$emit('updateManager')
    },
    addButton() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async getDepartmenChange() {
      //  部门负责人列表
      const res = await getDepartmentList()
      // console.log(res.data)
      this.list = res.data
    },
    hClose() {
      this.form = {
        name: '',
        code: '',
        managerId: '',
        introduce: '',
        pid: ''
      }
      // 清空表单
      this.$refs.form.resetFields()
      // console.log('用户点击关闭x')
      // 通知父组件修改showDialog
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
